var msgId = localStorage.getItem("sendedMsgId");
var sm = new Vue({
    el: "#messagestatistic",
    data: function () {
        return {
            visible: true
        }
    }
})


/********查阅信息********/
var consultTotal = getConsultOrConfirmCount(msgId, 0, 1);
var consulted = getConsultOrConfirmCount(msgId, 1, 1);
var consultRate = 0;
if (consultTotal != 0) {
    consultRate = (consulted / consultTotal * 100).toFixed(1);
}
var consultData = [{value: consulted, name: '已查阅'}, {value: (consultTotal - consulted), name: '未查阅'}]

/********确认信息********/
var confirmTotal = getConsultOrConfirmCount(msgId, 0, 2);
var confirmed = getConsultOrConfirmCount(msgId, 2, 2);
var confirmRate = 0;
if (confirmTotal != 0) {
    confirmRate = (confirmed / confirmTotal * 100).toFixed(1);
}
var confirmData = [{value: confirmed, name: '已确认'}, {value: (confirmTotal - confirmed), name: '未确认'}];

/********发送信息********/
var sendSuccess = getSendMessageCount(msgId, 1);
var sendFailed = getSendMessageCount(msgId, 9);
var sendRate = 0;
if (sendSuccess != 0) {
    sendRate = (sendSuccess / (sendSuccess + sendFailed) * 100).toFixed(1);
}
var sendData = [{value: sendSuccess, name: '发送成功'}, {value: sendFailed, name: '发送失败'}];

/********回复信息********/
var replyed = getReplyMessageCount(msgId, 0);
var replyTotal = getReplyMessageCount(msgId, 1);
var replyRate = 0;
if (replyTotal != 0) {
    replyRate = (replyed / replyTotal * 100).toFixed(1);
}
var replyData = [{value: replyed, name: '已回复'}, {value: (replyTotal - replyed), name: '未回复'}];

var consultCount = [consultTotal, consulted, (consultTotal - consulted), consultRate];
var confirmCount = [confirmTotal, confirmed, (confirmTotal - confirmed), confirmRate];
var sendCount = [(sendSuccess + sendFailed), sendSuccess, sendFailed, sendRate];
var replyCount = [replyTotal, replyed, (replyTotal - replyed), replyRate];

function initTable(id, data) {
    var trs = $("#" + id).find("tr");
    for (var i = 0; i < 3; i++) {
        $(trs[i]).find("td:odd").html(data[i] + "人");
    }
    $(trs[3]).find("td:odd").html(data[3] + "%");
}

initTable("consult-tab", consultCount);
initTable("confirm-tab", confirmCount);
initTable("send-tab", sendCount);
initTable("reply-tab", replyCount);

/**
 * 利用echarts插件生成饼状图
 * @param id
 * @param text
 * @param subtext
 * @param data
 */
function initChart(id, text, subtext, data) {
    var chartItem = echarts.init(document.getElementById(id));
    option = {
        title: {
            text: text,
            subtext: subtext + "%",
            left: 'center',
            top: '42%',
            itemGap: 5,
            textStyle: {
                color: '#141f1f',
                fontSize: 12
            },
            subtextStyle: {
                color: '#0072e6',
                fontSize: 18,
                fontWeight: 'bold'
            }
        },
        color: ['#0072e6', '#d7d7c1'],
        series: [
            {
                name: '消息',
                type: 'pie',
                radius: ['41%', '50%'],
                label: {
                    normal: {
                        formatter: '{b|{b}}\n{c}人',
                        rich: {
                            b: {
                                fontSize: 12,
                                lineHeight: 20
                            }
                        }
                    }
                },
                data: data
            }
        ]
    };
    chartItem.setOption(option);
}

/********饼图初始化********/
initChart("msg-consult-chart", "查阅率", consultRate, consultData);
initChart("msg-confirm-chart", "确认率", confirmRate, confirmData);
initChart("msg-send-chart", "成功率", sendRate, sendData);
initChart("msg-reply-chart", "回复率", replyRate, replyData);


/**
 * 获取查阅或确认信息相关记录数
 * @param msgId
 * @param status  1 查阅 2 确认
 * @param tag  1 查阅 2 确认
 * @returns {number}
 */
function getConsultOrConfirmCount(msgId, status, tag) {
    var ajaxData = 0;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/sendedmsg/getReceiptMsgCount",
        async: false,
        data: {
            msgId: msgId,
            receiptStatus: status,
            tag: tag
        }, success: function (data) {
            ajaxData = data.resultData;
        }
    });
    return ajaxData;
}

/**
 * 获取发送消息相关记录数
 * @param msgId
 * @param status  1 发送成功 9 发送失败
 * @returns {number}
 */
function getSendMessageCount(msgId, status) {
    var ajaxData = 0;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/sendedmsg/getReceiveMsgCount",
        async: false,
        data: {
            msgId: msgId,
            receiveStatus: status
        }, success: function (data) {
            ajaxData = data.resultData;
        }
    });
    return ajaxData;
}

/**
 * 获取回复消息相关记录数
 * @param msgId
 * @param status  0 已回复 1 全部
 * @returns {number}
 */
function getReplyMessageCount(msgId, status) {
    var ajaxData = 0;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/sendedmsg/getReplyMsgCount",
        async: false,
        data: {
            msgId: msgId,
            status: status
        }, success: function (data) {
            ajaxData = data.resultData;
        }
    });
    return ajaxData;
}


/********点击表格跳转响应界面********/
$("#consult-tab").find("tr").click(function () {
    $("#content").load("templates/authority/sendedMessage/html/messageConsultStatistic.html");
})
$("#confirm-tab").find("tr").click(function () {
    $("#content").load("templates/authority/sendedMessage/html/messageConfirmStatistic.html");
})
$("#send-tab").find("tr").click(function () {
    $("#content").load("templates/authority/sendedMessage/html/messageSendStatistic.html");
})
$("#reply-tab").find("tr").click(function () {
    $("#content").load("templates/authority/sendedMessage/html/messageReplyStatistic.html");
})




